package com.burry.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import applelew.util.Debug;

import com.burry.db.SQLMachine;
import com.burry.util.MapList;

/**
 * 分类管理相关
 * s_config = 1 为 伪分类
 * @author yang.liu
 *
 */
public class CatalogService {
	
	//todo 同时要更新内存数据
	public static MapList allFaCataInfo = new MapList();
	public static Map allCataInfo = new HashMap();
	public static List allCataList = new ArrayList();
	
	//public static List allSpList = new ArrayList();
	
	static{
		getAllCata();
	}
	
	public static void reload(){
		allFaCataInfo = new MapList();
		allCataInfo = new HashMap();
		allCataList = new ArrayList();
		getAllCata();
	}
	
	/**
	 * * msg:"SUCCESS" 成功 否则为相应的错误信息
	 * 	effectNum: Integer 影响的数据数目 如果发生任何错误该字段返回0,保证不会为空
	 * @param fatherCode
	 * @param cataCode
	 * @param cataName
	 * @return
	 */
	public Map addACatalog(String fatherCode,String cataCode,String cataName,String order,String isFake){
		Integer fatherDepth = getDepth(fatherCode);
		if(!fatherDepth.equals(new Integer(-1))){
			fatherDepth = new Integer(fatherDepth.intValue() + 1);
		}
		SQLMachine sqlM = new SQLMachine("default");
		String sql = "insert into bu_good_catalog(cata_code,cata_name,fa_code,cata_depth,view_order,s_config) values('" +cataCode+ "','"+ cataName +"','" + fatherCode+ "',"+fatherDepth+",'"+order+"','"+isFake+"')";
		return sqlM.updateResult(sql);
	}
	
	public Map delACatalog(String uid){
		SQLMachine sqlM = new SQLMachine("default");
		String sql = "delete from bu_good_catalog where cata_code = '"+uid+"'";
		sqlM.updateResult(sql);
		sql = "delete from bu_good_catalog where fa_code = '"+uid+"'";
		return sqlM.updateResult(sql);
	}
	
	/**
	 * 获取分类的级别，若没有取到，返回-1
	 * @param code
	 * @return
	 */
	public Integer getDepth(String code){
		Map tmp = (Map)CatalogService.allCataInfo.get(code);
		if(tmp == null)
			return new Integer(-1);
		return (Integer)tmp.get("cata_depth");
	}
	
	public static void getAllCata(){
		SQLMachine sqlM = new SQLMachine("default");
		List cataList = sqlM.getResultSet("select * from bu_good_catalog order by view_order");

		for(int i=0;i<cataList.size();i++){
			Map tmp = (Map)cataList.get(i);
			String upper = (String)tmp.get("fa_code");
			String code = (String)tmp.get("cata_code");
//			//处理多个父分类的问题
//			if(upper.indexOf(",") != -1){
//				String[] uppers = upper.split(",");
//				for(int j=0;j<uppers.length;j++){
//					allFaCataInfo.put(uppers[j], tmp);
//				}
//				
//				allCataInfo.put(code, tmp);
//				
//			}else{
//				allCataInfo.put(code, tmp);
//				allFaCataInfo.put(upper, tmp);
//			}
			allCataInfo.put(code, tmp);
			allFaCataInfo.put(upper, tmp);
			
			
		}
		
		allCataList = cataList;
		
//		List sList = sqlM.getResultSet("select cata_code from bu_good_catalog where s_config = '3'");
//		if(sList != null){
//			for(int i=0;i<sList.size();i++){
//				Map t = (Map)sList.get(i);
//				allSpList.add(t.get("cata_code"));
//			}
//		}
	}
	
	public String getTreeStr(MapList ml){
		StringBuffer sb = new StringBuffer();
		List root = (List)ml.get("-1");
		for(int i=0;i<root.size();i++){
			Map tmp = (Map)root.get(i);
			//String upper = (String)tmp.get("fa_code");
			String code = (String)tmp.get("cata_code");
			String name = (String)tmp.get("cata_name");
			String key = code+"_1";
			sb.append("var uid"+key+" = '" + code + "';");
			sb.append("var item" +key+ " = m_wsGridCtrl.InsItem(null, \"" +code+ ";" +name+ ";<input type=button value='新增' onclick='addCata(uid"+key+")' /> &nbsp <input type=button value='修改' onclick='updateCata(uid"+key+")' />&nbsp <input type=button value='删除' onclick='deleteCata(uid"+key+")' />&nbsp <input type=button value='绑定广告' onclick='bindAd(uid"+key+")' />;\", \"#;#;#;#;\", \"_self;_self;_self;_self;\");");
			
//			List sonList = (List)ml.get(code);
//			if(sonList == null)
//				sonList = new ArrayList();
//			for(int j=0;j<sonList.size();j++){
//				Map t = (Map)sonList.get(j);
//				String up = (String)t.get("fa_code");
//				String co = (String)t.get("cata_code");
//				String na = (String)t.get("cata_name");
//				
//				sb.append("var uid"+i+ j + " = '" + co + "';");
//				sb.append("m_wsGridCtrl.InsItem(item"+i+", \"" +co+ ";" +na+ ";<input type=button value='新增' onclick='addCata(uid"+i + j +")' /> &nbsp <input type=button value='修改' onclick='updateCata(uid"+i+ j + ")' />&nbsp <input type=button value='删除' onclick='deleteCata(uid"+i+ j + ")' />;\", \"#;#;#;\", \"_self;_self;_self;\");");
//			}
			String result = getTreeStrByCode(ml,code,key);
			sb.append(result);
		}
		return sb.toString();
	}
	
	public String getTreeStr2(MapList ml){
		StringBuffer sb = new StringBuffer();
		List root = (List)ml.get("-1");
		for(int i=0;i<root.size();i++){
			Map tmp = (Map)root.get(i);
			//String upper = (String)tmp.get("fa_code");
			String code = (String)tmp.get("cata_code");
			String name = (String)tmp.get("cata_name");
			String key = code+"_1";
			sb.append("var uid"+key+" = '" + code + "';");
			sb.append("var item" +key+ " = m_wsGridCtrl.InsItem(null, \"" +code+ ";" +name+ ";<input type=button value='录入数量' onclick='addCata(uid"+key+")' /> &nbsp ;\", \"#;#;#;#;\", \"_self;_self;_self;_self;\");");
			
//			List sonList = (List)ml.get(code);
//			if(sonList == null)
//				sonList = new ArrayList();
//			for(int j=0;j<sonList.size();j++){
//				Map t = (Map)sonList.get(j);
//				String up = (String)t.get("fa_code");
//				String co = (String)t.get("cata_code");
//				String na = (String)t.get("cata_name");
//				
//				sb.append("var uid"+i+ j + " = '" + co + "';");
//				sb.append("m_wsGridCtrl.InsItem(item"+i+", \"" +co+ ";" +na+ ";<input type=button value='新增' onclick='addCata(uid"+i + j +")' /> &nbsp <input type=button value='修改' onclick='updateCata(uid"+i+ j + ")' />&nbsp <input type=button value='删除' onclick='deleteCata(uid"+i+ j + ")' />;\", \"#;#;#;\", \"_self;_self;_self;\");");
//			}
			String result = getTreeStrByCode2(ml,code,key);
			sb.append(result);
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * @param ml
	 * @param code
	 * @param key 子编号+父编号
	 * @return
	 */
	private String getTreeStrByCode(MapList ml , String code , String key){
		StringBuffer sb = new StringBuffer();
		
		List sonList = (List)ml.get(code);
		if(sonList == null)
			return sb.toString();
		for(int j=0;j<sonList.size();j++){
			Map t = (Map)sonList.get(j);
			String up = (String)t.get("fa_code");
			String co = (String)t.get("cata_code");
			String na = (String)t.get("cata_name");
			String myKey = co + up; 
			sb.append("var uid"+myKey+ " = '" + co + "';");
			sb.append("var item" +myKey+ " = m_wsGridCtrl.InsItem(item"+key+", \"" +co+ ";" +na+ ";<input type=button value='新增' onclick='addCata(uid"+myKey +")' /> &nbsp <input type=button value='修改' onclick='updateCata(uid"+myKey + ")' />&nbsp <input type=button value='删除' onclick='deleteCata(uid"+myKey + ")' />&nbsp <input type=button value='绑定广告' onclick='bindAd(uid"+myKey+")' />;\", \"#;#;#;#;\", \"_self;_self;_self;_self;\");");
			String result = getTreeStrByCode(ml,co,myKey);
			sb.append(result);
		}
		
		return sb.toString();
	}
	
	private String getTreeStrByCode2(MapList ml , String code , String key){
		StringBuffer sb = new StringBuffer();
		
		List sonList = (List)ml.get(code);
		if(sonList == null)
			return sb.toString();
		for(int j=0;j<sonList.size();j++){
			Map t = (Map)sonList.get(j);
			String up = (String)t.get("fa_code");
			String co = (String)t.get("cata_code");
			String na = (String)t.get("cata_name");
			String myKey = co + up; 
			sb.append("var uid"+myKey+ " = '" + co + "';");
			sb.append("var item" +myKey+ " = m_wsGridCtrl.InsItem(item"+key+", \"" +co+ ";" +na+ ";<input type=button value='录入数量' onclick='addCata(uid"+myKey +")' /> &nbsp ;\", \"#;#;#;#;\", \"_self;_self;_self;_self;\");");
			String result = getTreeStrByCode2(ml,co,myKey);
			sb.append(result);
		}
		
		return sb.toString();
	}
	
	
	public List getCataAd(String cataCode){
		String sql = "select * from bu_cata_ad where cata_code = '"+cataCode+"'";
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.getResultSet(sql);
		
	}
	
	public Map addOrUpdateAd(String code,String adContent,String isNav,String isUpdate){
		String sql = null;
		if("true".equals(isUpdate)){
			sql = "update bu_cata_ad set ad_content = '"+adContent+"',isNav = '"+isNav+"' where cata_code = '"+code+"'";
		}else{
			sql = "insert into bu_cata_ad(cata_code,ad_content,isNav) values('"+code+"','"+adContent+"','"+isNav+"')";
		}
		SQLMachine sqlM = new SQLMachine("default");
		return sqlM.updateResult(sql);
	}
	
	public Map updateCatalog(String id,String pid,String name,String order,String isFake){
		SQLMachine sqlM = new SQLMachine("default");
		String sql = "update bu_good_catalog set cata_name = '"+name+"',view_order='"+order+"',s_config = '"+isFake+"' where cata_code = '"+id+"' and fa_code = '"+pid+"' ";
		return sqlM.updateResult(sql);
	}
	
	public boolean isListGood(String cata){
		GoodService gs = new GoodService();
		List result = gs.getGoodByCata(cata);
		if(result != null && result.size() != 0)
			return true;
		return false;
	}
	
	public String getChildStr(String cata){
		StringBuffer sb = new StringBuffer();
		List allChild = (List)allFaCataInfo.get(cata);
		
		if(allChild == null){
			sb.append("'"+cata+"'");
			return sb.toString();
		}
		for(int i=0;i<allChild.size();i++){
			Map tmp = (Map)allChild.get(i);
			String cataCode = (String)tmp.get("cata_code");
			sb.append("'"+cataCode+"',");
			List child = (List)allFaCataInfo.get(cataCode);
			if(child != null && child.size() != 0){
				for(int j=0;j<child.size();j++){
					Debug.logInfo("j==> " + j);
					Map t = (Map)child.get(j);
					Debug.logInfo("t==> " + t);
					String cCode = (String)t.get("cata_code");
					sb.append("'"+cCode+"',");
				}
			}
		}
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
	}
	
	public List getFatherList(String cata){
		List result = new ArrayList();
		Map cataMap = (Map)allCataInfo.get(cata);
		String s_config = (String)cataMap.get("s_config");
		String father = (String)cataMap.get("fa_code");
		if("-1".equals(father)){
			return result;
		}else{
			if("1".equals(s_config)){
				result.add(cata);
				List fatherList = getFatherList(father);
				result.addAll(fatherList);
			}else{
				result.add(cata);
				List fatherList = getFatherList(father);
				result.addAll(fatherList);
			}
			
		}
		
		return result;
	}
	
	public String getFatherTwo(String cata){
		List fatherList = getFatherList(cata);
		if(fatherList.size() < 2)
			return cata;
		return (String)fatherList.get(fatherList.size()-1);
	}
	
	public String getFatherThree(String cata){
		List fatherList = getFatherList(cata);
		if(fatherList.size() < 3)
			return cata;
		return (String)fatherList.get(fatherList.size()-2);
	}
	
	public String getFatherFour(String cata){
		List fatherList = getFatherList(cata);
		if(fatherList.size() < 4)
			return cata;
		return (String)fatherList.get(fatherList.size()-3);
	}
	
	
}
